package com.aliang.book_java1.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.http.HttpStatus;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 检查session中是否有用户登录信息
        if (request.getSession().getAttribute("username") != null) {
            return true; // 用户已登录，继续执行
        } else {
            response.setStatus(HttpStatus.UNAUTHORIZED.value()); // 用户未登录，返回401错误
            return false;
        }
    }
}
